package br.fatec.ads.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.fatec.ads.dao.RedeSocialDAO;
import br.fatec.ads.exception.DataBaseException;
import br.fatec.ads.exception.RedeSocialException;
import br.fatec.ads.model.RedeSocial;

/**
 * Servlet implementation class RedeSocialIncluir
 */
public class RedeSocialIncluir extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public RedeSocialIncluir() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("cadredesocial_i.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			HttpSession session = request.getSession(false);
			
			RedeSocial redeSocial = new RedeSocial();
			redeSocial.setDescRedeSocial(request.getParameter("txtRedeSocial"));
			redeSocial.setUsuario(session.getAttribute("usuarioDesc").toString());
			
			RedeSocialDAO redeSocialDAO = new RedeSocialDAO();
			
			redeSocialDAO.incluirRedeSocial(redeSocial);
			
			request.getRequestDispatcher("RedeSocialPesquisar?acao=SUCESSO_INCLUIR").forward(request, response);
		} catch (RedeSocialException e) {
			request.getRequestDispatcher("cadredesocial_i.jsp?acao=erro").forward(request, response);
			e.printStackTrace();
		} catch (DataBaseException e) {
			request.getRequestDispatcher("cadredesocial_i.jsp?acao=erro").forward(request, response);
			e.printStackTrace();
		}
	}
}
